import java.io.FileOutputStream;
import java.util.Scanner;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class testBench {
    public static void main(String[] args) throws Exception {
        schoolBase modle = (schoolBase) new model();
        String verify = null;

        // 更改此处为你的账号密码进行测试
        String id = "000000", pas = "000000";
        if (modle.needVerifyCode) {
            byte[] img = modle.getVerifyCode();
            try {
                FileOutputStream out = new FileOutputStream("verifyCode.jpg"); // 可以自行更改后缀
                out.write(img);
                out.flush();
                out.close();

                // 若需要验证码将会暂停，在控制台输入验证码后继续进行测试，验证码保存在当前目录
                Scanner in = new Scanner(System.in);
                verify = in.next();
                in.close();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        String LoginRes = modle.Login(id, pas, verify);
        System.out.println("checkLogin: " + checkLogin(LoginRes));

        String getCourseRes = modle.getCourses();
        System.out.println("checkGetCourse: " + checkGetCourse(getCourseRes));

        String getExamRes = modle.getExams();
        System.out.println("checkGetExam: " + checkGetExam(getExamRes));

        String getGPARes = modle.getGPA();
        System.out.println("checkGetGPA: " + checkGetGPA(getGPARes));

        String getScoreRes = modle.getScores();
        System.out.println("checkGetScore: " + checkGetScore(getScoreRes));
    }

    private static String checkLogin(String LoginRes) {
        try {
            JSONObject res = (JSONObject) (new JSONParser().parse(LoginRes));
            if (res.containsKey("name") && res.get("name").getClass().getName().equals("java.lang.String"))
                return "true";
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return "fail";
    };

    private static String checkGetCourse(String getCourseRes) {
        try {
            JSONObject res = (JSONObject) (new JSONParser().parse(getCourseRes));
            if (!res.containsKey("startDate") || !res.get("startDate").getClass().getName().equals("java.lang.String"))
                return "fail";
            if (!res.containsKey("courses"))
                return "fail";
            JSONArray courses = (JSONArray) res.get("courses");
            if (courses.size() != 7)
                return "fail";
            for (int i = 0; i < 7; i++) {
                JSONArray day = (JSONArray) courses.get(i);
                for (Object c : day) {
                    JSONObject _c = (JSONObject) c;
                    if (!_c.containsKey("name") || !_c.get("name").getClass().getName().equals("java.lang.String"))
                        return "fail";
                    if (!_c.containsKey("idx") || !_c.get("idx").getClass().getName().equals("java.lang.Long"))
                        return "fail";
                    if (!_c.containsKey("last") || !_c.get("last").getClass().getName().equals("java.lang.Long"))
                        return "fail";
                    if (!_c.containsKey("teacher")
                            || !_c.get("teacher").getClass().getName().equals("java.lang.String"))
                        return "fail";
                    if (!_c.containsKey("place") || !_c.get("place").getClass().getName().equals("java.lang.String"))
                        return "fail";
                    if (!_c.containsKey("week"))
                        return "fail";
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            return "fail";
        }
        return "true";
    };

    private static String checkGetExam(String getExamRes) {
        try {
            JSONArray exams = (JSONArray) (new JSONParser().parse(getExamRes));
            String dateReg = "\\d{4}-\\d{2}-\\d{2}";
            String timeReg = "\\d{2}:\\d{2}~\\d{2}:\\d{2}";
            for (Object e : exams) {
                JSONObject _e = (JSONObject) e;
                if (!_e.containsKey("name") || !_e.get("name").getClass().getName().equals("java.lang.String"))
                    return "fail";
                if (!_e.containsKey("startDate") || !_e.get("startDate").getClass().getName().equals("java.lang.String")
                        || !((String) _e.get("startDate")).matches(dateReg))
                    return "fail";
                if (!_e.containsKey("place") || !_e.get("place").getClass().getName().equals("java.lang.String"))
                    return "fail";
                if (!_e.containsKey("time") || !_e.get("time").getClass().getName().equals("java.lang.String")
                        || !((String) _e.get("time")).matches(timeReg))
                    return "fail";
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            return "fail";
        }
        return "true";
    };

    private static String checkGetGPA(String getGPARes) {
        try {
            JSONObject gpa = (JSONObject) (new JSONParser().parse(getGPARes));
            if (!gpa.containsKey("gpa") || !gpa.get("gpa").getClass().getName().equals("java.lang.Double"))
                return "fail";
            if (!gpa.containsKey("all") || !gpa.get("all").getClass().getName().equals("java.lang.Long"))
                return "fail";
            if (!gpa.containsKey("at") || !gpa.get("at").getClass().getName().equals("java.lang.Long"))
                return "fail";
        } catch (Exception e) {
            System.out.println(e.toString());
            return "fail";
        }
        return "true";
    };

    private static String checkGetScore(String getScoreRes) {
        try {
            JSONArray scores = (JSONArray) (new JSONParser().parse(getScoreRes));
            for (Object s : scores) {
                JSONObject _s = (JSONObject) s;
                if (!_s.containsKey("name") || !_s.get("name").getClass().getName().equals("java.lang.String"))
                    return "fail";
                if (!_s.containsKey("value") || !(_s.get("value").getClass().getName().equals("java.lang.String") || _s
                        .get("value").getClass().getName().equals("java.lang.Long")))
                    return "fail";
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            return "fail";
        }
        return "true";
    };
}
